package cn.bingo.yunsj.controller;

import bingo.link.sdk.handler.es.EsServiceHandler;
import bingo.link.sdk.handler.uam.UamOrganizationHandler;
import bingo.link.sdk.handler.uam.UamUserHandler;
import bingo.link.sdk.model.common.PageInfo;
import bingo.link.sdk.model.common.PageList;
import bingo.link.sdk.model.uam.UamOrganization;
import bingo.link.sdk.model.uam.UamUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * 企业服务的控制器
*/
@Controller
@RequestMapping("/EsService")
@CrossOrigin
public class EsServiceController
{
    private EsServiceHandler esServiceHandler;

    private UamOrganizationHandler uamOrganizationHandler;

    private UamUserHandler uamUserHandler;

    public EsServiceController()
    {
        if (esServiceHandler == null)
        {
            this.esServiceHandler = new EsServiceHandler();
        }
        if(uamOrganizationHandler == null){
            this.uamOrganizationHandler = new UamOrganizationHandler();
        }
        if(uamUserHandler == null){
            this.uamUserHandler = new UamUserHandler();
        }
    }

    /**
     * 获取所有部门
     * @return
     */
    @RequestMapping(value = "/listEsService", method = RequestMethod.GET)
    public ModelAndView listEsService()
    {
        PageInfo pageInfo = new PageInfo(1,10);
        PageList<UamOrganization> allInternalOrganization = this.uamOrganizationHandler.getAllInternalOrganization(pageInfo);
        allInternalOrganization.getResultSet().forEach(e->{
            System.out.println(e.getName()+" "+e.getOrgId()+" ");
        });
        return new ModelAndView("index").addObject("testResult", allInternalOrganization);
    }

    /**
     * 获取所有有效人员信息
     * @return
     */
    @RequestMapping(value = "/listEsService1", method = RequestMethod.GET)
    public ModelAndView listEsService1()
    {
        PageInfo pageInfo = new PageInfo(1,10);
        PageList<UamUser> allUsers = this.uamUserHandler.getAllUsers(pageInfo);
        System.out.println(allUsers.size());
        return new ModelAndView("index").addObject("testResult", allUsers);
    }

    /**
     * 获取当前用户
     */
    @RequestMapping(value = "/getCurrentUser",method = RequestMethod.GET)
    public void getCurrentUser(){
        UamUser currentUser = this.uamUserHandler.getCurrentUser();
        System.out.println(currentUser.getName());
    }

    /**
     * 获取部门详细信息
     */
    @RequestMapping(value = "/detailOrganization",method = RequestMethod.GET)
    public ModelAndView detailOrganization(){
        UamOrganization uamOrganization = this.uamOrganizationHandler.detailOrganization("c07b016b-369c-485d-8b2e-32db0fcf05e5");
        System.out.println(uamOrganization.toString());
        return new ModelAndView("index").addObject("testResult", uamOrganization);
    }

    /**
     * 根据用户id查询用户
     */
    @RequestMapping(value = "/getUserById",method = RequestMethod.GET)
    public ModelAndView getUSer(){
        UamUser userById = this.uamUserHandler.getUserById("89730e01-0bde-4b7c-87dd-57b3609815d4");
        System.out.println(userById.getName());
        return new ModelAndView("index").addObject("testResult", userById);
    }

    /**
     * 根据组织id获取用户
     */
    @RequestMapping(value = "/getUserByOrganizationIdOnlyOwn",method = RequestMethod.GET)
    public ModelAndView getUserByOrganizationIdOnlyOwn(){
        PageInfo pageInfo = new PageInfo(1,10);
        PageList<UamUser> userByOrganizationIdOnlyOwn = this.uamUserHandler.getUserByOrganizationIdOnlyOwn("c07b016b-369c-485d-8b2e-32db0fcf05e5",pageInfo);
        userByOrganizationIdOnlyOwn.getResultSet().forEach(e->{
            System.out.println(e.getName());
        });
        return new ModelAndView("index").addObject("testResult", userByOrganizationIdOnlyOwn);
    }

    /**
     * 根据组织id获取用户
     */
    @RequestMapping(value = "/getAllUsers",method = RequestMethod.GET)
    public ModelAndView getAllUsers(){
        PageInfo pageInfo = new PageInfo(1,10);
        PageList<UamUser> userByOrganizationIdOnlyOwn = this.uamUserHandler.getAllUsers(pageInfo);
        userByOrganizationIdOnlyOwn.getResultSet().forEach(e->{
            System.out.println(e.getName());
        });
        return new ModelAndView("index").addObject("testResult", userByOrganizationIdOnlyOwn);
    }
}